<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    p {
      margin-bottom: 3px;
      height: 30px;
      line-height: 30px;
      /* background: yellow; */
    }

    label {
      float: left;
      margin-right: 10px;
      width: 100px;
      height: 30px;
      text-align: right;
    }

    input {
      float: left;
      border: 0;
      height: 30px;
      line-height: 30px;
      width: 200px;
      background: pink;
      outline: none;
    }

    p span {
      margin-left: 3px;
      display: inline-block;
      height: 30px;
      line-height: 30px;
      background: green;
      text-align: center;
    }

    #login {
      margin-left: 50px;
      width: 40px;
      height: 30px;
      line-height: 30px;
      text-align: center;
    }
  </style>
</head>

<body>
  <p>
    <label for="tel">手机号：</label><input type="text" id="tel"><span class="tel-tip"></span>
  </p>
  <p>
    <label for="pwd">密码：</label><input type="password" id="pwd"><span class="pwd-tip"></span>
  </p>
  <p>
    <label for="yzm">验证码：</label><input type="number" id="yzm"><span class="yzm-tip">1234</span>
  </p>
  <!-- <button id="login">登录</button> -->
  <script>
    // 需求：验证码匹配的问题 
    //1：获取元素
    let tel = document.querySelector("#tel") //手机
    let pwd = document.querySelector("#pwd") //密码
    let yzm = document.querySelector("#yzm") //验证码
    let tel_tip = document.querySelector(".tel-tip"); //手机的提示信息
    let pwd_tip = document.querySelector(".pwd-tip"); //密码的提示信息
    let yzm_tip = document.querySelector(".yzm-tip"); //验证码的提示信息
    let login = document.querySelector("#login")

    //手机号码失去焦点验证
    let reg1 = /^[1][0-9]{10}$/
    let telVal = tel.value; //手机号码的值
    tel.onblur = function () {
      if (reg1.test(telVal)) {
        tel_tip.innerHTML = "手机号码输入正确"
      } else {
        tel_tip.innerHTML = "请输入11位数的手机号码"
      }
    }

    //密码失去焦点验证
    let pwdVal = pwd.value; //密码的值
    let reg2 = /^[0-9A-Za-z]{6,10}$/
    pwd.onblur = function () {
      if (reg2.test(pwdVal)) {
        pwd_tip.innerHTML = "密码格式输入正确"
      } else {
        pwd_tip.innerHTML = "请输入6到10为的密码"
      }
    }

    //验证码事件
    yzm.addEventListener("blur", function () {
      let yzmVal = yzm.value; //验证码的值
      var yzmTip = yzm_tip.innerHTML; //验证码提示的值
      if (yzmVal != yzmTip) {
        alert("验证码输入不正确")
        yzm_tip.innerHTML = randomNum(1000, 9999); //随机数
      } else {
        alert("验证码输入正确")
      }
    })

    //验证码的跟换随机数
    yzm_tip.onclick = function () {
      yzm_tip.innerHTML = randomNum(1000, 9999); //随机数
    }

    function randomNum(n, m) {
      let min = n < m ? n : m; //最小值
      let max = n > m ? n : m //最大值
      return parseInt((Math.random() * (max - min + 1) + min))
    }
  </script>
</body>

</html>